<?php

declare(strict_types=1);
/**
 * 访问 
 * - http://localhost:8080/utils/date
 * - http://localhost:8080/utils/time
 * 
 * 输出
 * - It is now 2021-11-16 14:51:23. Enjoy!
 * - It is now 1636991883. Enjoy!
 */
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Routing\RouteCollectorProxy;

$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
    $response->getBody()->write('Hello World');
    return $response;
});

$app->group('/utils', function (RouteCollectorProxy $group) {
    $group->get('/date', function (ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
        $response->getBody()->write(date('Y-m-d H:i:s'));
        return $response;
    });

    $group->get('/time', function (ServerRequestInterface $request, ResponseInterface $response): ResponseInterface {
        $response->getBody()->write((string) time());
        return $response;
    });
})->add(function (ServerRequestInterface $request, RequestHandlerInterface $handler) use ($app) {
    $response = $handler->handle($request);
    $dateOrTime = (string) $response->getBody();

    $response = $app->getResponseFactory()->createResponse();
    $response->getBody()->write('It is now ' . $dateOrTime . '. Enjoy!');

    return $response;
});